package BankingManagement;

/**
 *  use ArrayList to set the priority
 * @author ashongtical
 */
// import necessary packages
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class TaskManager {
    private ArrayList highPriority;
    private HashMap<String, String> lowPriority;
    //use ArrayList to manage the data

    public TaskManager() {
        highPriority = new ArrayList<>();
        lowPriority = new HashMap<>();
       //create new ArrayList object for priorities
    }

    // Add a task
    public void addTask(String task) {
        if (task.endsWith("*")) {
            highPriority.add(task);
        } else {
            lowPriority.put(task,task);
        }
        
    }

    // Remove a task
    public void removeTask(String priority, int index) {
        if ("high".equals(priority) && index >= 0 && index < highPriority.size()) {
            highPriority.remove(index);
        } else if ("low".equals(priority) && index >= 0 && index < lowPriority.size()) {
            // Remove tasks from low-priority tasks list
            List<String> lowPriorityTasks = new ArrayList<>(lowPriority.keySet());
            String taskToRemove = lowPriorityTasks.get(index);
            lowPriority.remove(taskToRemove);
        }
    }

    // Change task priority
    // allows moving tasks between high and low priority lists
    public void changePriority(String priority, int index) {
        if ("high".equals(priority) && index >= 0 && index < highPriority.size()) {
            // Move the high-priority task to the low-priority one and remove *
            String task = (String) highPriority.remove(index);
            String taskWithoutStar = task.endsWith("*") ? task.substring(0, task.length() - 1).trim() : task;
            lowPriority.put(taskWithoutStar, taskWithoutStar);
        } else if ("low".equals(priority) && index >= 0 && index < lowPriority.size()) {
            // Move the low-priority task to the high-priority one and add *
            List<String> lowPriorityTasks = new ArrayList<>(lowPriority.keySet());
            String task = lowPriorityTasks.get(index);
            lowPriority.remove(task);
            highPriority.add(task + "*");
        }
    }

    // Promote a high-priority task 
    //moves a high-priority task up in the list
    public void promoteTask(int index) {
        if (index > 0 && index < highPriority.size()) {
            // Swap the positions of the current task and the previous one
            String temp = (String) highPriority.get(index);
            highPriority.set(index, highPriority.get(index - 1));
            highPriority.set(index - 1, temp);
        }
        
    }
    
    //list getters
    // Get high-priority tasks
    // return copies of the lists to avoid direct modification
    public ArrayList<String> getHighPriorityTasks() {
        return new ArrayList<>(highPriority);
    }

    // Get low-priority tasks
    // return copies of the lists to avoid direct modification
    public ArrayList<String> getLowPriorityTasks() {
        return new ArrayList<>(lowPriority.keySet());
    }

    // Display tasks
    public void displayTasks() {
        System.out.println("\nHigh Priority Tasks:");
        for (int i = 0; i < highPriority.size(); i++) {
            System.out.println(i + ". " + highPriority.get(i));
        }

        System.out.println("\nLow Priority Tasks:");
        for (String task : lowPriority.keySet()) {
            System.out.println(task);
        }
    }
}
